package com.songaw.cangku.controller;


import com.songaw.cangku.base.util.UuidUtils;
import com.songaw.cangku.mapper.AppAccountExMapper;
import com.songaw.cangku.mapper.AppAccountMapper;
import com.songaw.cangku.pojo.PageInfo;
import com.songaw.cangku.pojo.Result;
import com.songaw.cangku.pojo.domain.AddAccountVo;
import com.songaw.cangku.pojo.domain.AppAccountVo;
import com.songaw.cangku.pojo.domain.UpdateAccountVo;
import com.songaw.cangku.pojo.entity.AppAccount;
import io.swagger.annotations.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.Date;
import java.util.List;

/**
 * @author zhaoxinguo on 2017/9/13.
 */
@Api(value = "用户管理接口", tags = { "用户管理接口" })
@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private AppAccountMapper appAccountMapper;

    @Autowired
    private AppAccountExMapper appAccountExMapper;


    /**
     * 注册用户 默认开启白名单
     * @param appAccountVo
     */
    @ApiOperation(value = "注册用户", notes = "注册用户")
    @ApiImplicitParam(name = "appAccountVo", value = "用户账号详细实体appAccount", required = true, dataType = "AddAccountVo")
    @ApiResponses({ @ApiResponse(code = 400, message = "请求参数没填好"), @ApiResponse(code = 404, message = "请求路径没有或页面跳转路径不对", response = Result.class) })
    @PostMapping("/signup")
    public Result signup(@RequestBody AddAccountVo appAccountVo) {
        AppAccountVo search = new AppAccountVo();
        search.setEmplno(appAccountVo.getEmplno());
        List<AppAccount> list =  appAccountExMapper.select(search);
        if(list!=null&&list.size()>0){
            return Result.getParamsErrorResult("用户已经存在");

        }
        AppAccount appAccount = new AppAccount();
        BeanUtils.copyProperties(appAccountVo,appAccount);
        appAccount.setUuid(UuidUtils.base58Uuid());
        appAccount.setDeleteFlag("0");
        long time = new Date().getTime();
        appAccount.setInsertTime(time);
        appAccount.setUpdateTime(time);
        appAccount.setPassword(DigestUtils.md5DigestAsHex((appAccount.getPassword()).getBytes()));
         appAccountMapper.insert(appAccount);
        return Result.getSuccessResult(appAccount);
    }




    @RequestMapping(path="/v1/appAccounts/{uuid}",method = RequestMethod.GET)
    public Result<AppAccount> findByLoginInfoUuid(@PathParam("uuid") String uuid){
        try {
            return Result.getSuccessResult(appAccountMapper.selectByPrimaryKey(uuid));
        }catch (Exception e){
            return Result.getSystemErrorResult(e.getMessage());
        }
    }
    @ApiOperation(value = "修改用户", notes = "修改用户")
    @ApiImplicitParam(name = "vo", value = "用户账号详细实体", required = true, dataType = "UpdateAccountVo")
    @ApiResponses({ @ApiResponse(code = 400, message = "请求参数没填好"), @ApiResponse(code = 404, message = "请求路径没有或页面跳转路径不对", response = Result.class) })
    @RequestMapping(path="/v1/appAccounts",method = RequestMethod.PUT)
    public Result<Integer> updateAppAccountAccount(@RequestBody UpdateAccountVo vo){
        try {
            AppAccount appAccount = new AppAccount();
            BeanUtils.copyProperties(vo,appAccount);
            appAccount.setPassword(DigestUtils.md5DigestAsHex((appAccount.getPassword()).getBytes()));
              appAccountMapper.updateByPrimaryKeySelective(appAccount);
            return Result.getSuccessResult(1);
        }catch (Exception e){
            return Result.getSystemErrorResult(e.getMessage());
        }
    }

    @RequestMapping(path="/v1/appAccounts",method = RequestMethod.DELETE)
    public Result<Integer> deleteAppAccountAccount(@RequestParam("uuid") String uuid){
        try {
            AppAccount c =appAccountMapper.selectByPrimaryKey(uuid);
            if(c!=null){
                c.setDeleteFlag("1");
                appAccountMapper.updateByPrimaryKey(c);
            }else{
                return Result.getSystemErrorResult("不存在");
            }

            return Result.getSuccessResult(1);
        }catch (Exception e){
            return Result.getSystemErrorResult(e.getMessage());
        }
    }
    @ApiOperation(value = "分页查询用户", notes = "分页查询用户")
    @ApiResponses({ @ApiResponse(code = 400, message = "请求参数没填好"), @ApiResponse(code = 404, message = "请求路径没有或页面跳转路径不对", response = Result.class) })
    @RequestMapping(path = "/v1/appAccounts/list/search",method = RequestMethod.GET)
    public Result<PageInfo> findAppAccountAccountsPage(@ModelAttribute AppAccountVo searchVo){
        PageInfo pageInfo = new PageInfo();
        List<AppAccount>  list=  appAccountExMapper.select(searchVo,searchVo.getRowBounds());
        int count = appAccountExMapper.selectCount(searchVo);
        pageInfo.setTotal(count);
        pageInfo.setResults(list);
        pageInfo.setPageIndex(searchVo.getPageIndex());
        pageInfo.setPageSize(searchVo.getPageSize());
        return Result.getSuccessResult(pageInfo);
    }
    @RequestMapping(path = "/v1/appAccounts/list",method = RequestMethod.GET)
    public Result<List<AppAccount>> findAppAccountAccounts(@ModelAttribute AppAccountVo searchVo){

        List<AppAccount>  list=  appAccountExMapper.select(searchVo);

        return Result.getSuccessResult(list);
    }
}
